package com.photography.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photography.annotation.RequireRole;
import com.photography.common.Result;
import com.photography.entity.Cart;
import com.photography.service.CartService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequireRole({"manager", "user"})
@RestController
@RequestMapping("/cart")
public class CartController {
    @Resource
    private CartService cartService;

    /**
     * 添加商品到购物车
     *
     * @param cart 购物车信息
     * @return 结果
     */
    @PostMapping("/add")
    public Result addToCart(@RequestBody Cart cart) {
        return cartService.addToCart(cart);
    }

    /**
     * 删除购物车记录
     *
     * @param id 购物车id
     * @return 结果
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteCart(@PathVariable Integer id) {
        return cartService.deleteCart(id);
    }

    /**
     * 清空用户购物车
     *
     * @return 结果
     */
    @DeleteMapping("/clear")
    public Result clearCart() {
        return cartService.clearCart();
    }

    /**
     * 分页查询购物车列表
     *
     * @param current 当前页码
     * @param size    每页大小
     * @return 结果
     */
    @GetMapping("/list")
    public Result getCartPage(@RequestParam(defaultValue = "1") Integer current,
                             @RequestParam(defaultValue = "10") Integer size) {
        Page<Cart> page = new Page<>(current, size);
        Page<Map<String, Object>> cartPage = cartService.getCartPage(page);
        return Result.success(cartPage, "查询成功");
    }

    @PutMapping("/update")
    public Result updateQuantity(@RequestParam(required = false) Integer id,
                                 @RequestParam(required = false) Integer quantity){
        return cartService.updateQuantity(id,quantity);
    }

    /**
     * 获取购物车总金额
     *
     * @return 结果
     */
    @GetMapping("/total")
    public Result getCartTotalAmount() {
        return cartService.getCartTotalAmount();
    }
}
